package com.zmj.sy.mom.srv.aps.controller;


import com.zmj.sy.mom.srv.aps.bean.entity.order.ApsBatch;
import com.zmj.sy.mom.srv.aps.bean.entity.order.Order;
import com.zmj.sy.mom.srv.aps.bean.entity.third.HexagonPackagePush;
import com.zmj.sy.mom.srv.aps.bean.vo.aps.*;
import com.zmj.sy.mom.srv.aps.bean.vo.apsorder.ApsOrderScheduleReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseListResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseResVo;
import com.zmj.sy.mom.srv.aps.service.ApsOrderService;
import com.zmj.sy.mom.srv.aps.service.ApsService;
import com.zmj.sy.mom.srv.aps.utils.SyExceptionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Api(tags = "aps")
@RestController
@RequestMapping("/aps")
public class ApsController {

    private final ApsService apsService;
    private final ApsOrderService apsOrderService;

    @ApiOperation("月度策划 -- 混合排产")
    @PostMapping("/monthSchedule")
    public BaseResVo monthSchedule(@RequestBody ApsMonthScheduleReqVo reqVo) {
        apsService.monthSchedule(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("APS高级排产")
    @PostMapping("/scheduling")
    public BaseResVo scheduling(@RequestBody ApsSchedulingBatchReqVo reqVo) {

        if (reqVo.getAssemblyStartDateTime() == null) {
            throw SyExceptionUtils.e("组件开始时间不能为空");
        }

        try {
            reqVo.setAssemblyStartDateTime(reqVo.getAssemblyStartDateTime());
            apsService.scheduling(reqVo);
        } finally {
            apsService.unlock();
        }
        return BaseResVo.ok();
    }

    @ApiOperation("纯倒序排产")
    @PostMapping("/reverseScheduling")
    public BaseResVo reverseScheduling(@RequestBody ApsReverseSchedulingReqVo reqVo) {
        apsService.reverseScheduling(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("订单重排")
    @PostMapping("/reScheduling")
    public BaseResVo reScheduling(@RequestBody ApsOrderScheduleReqVo reqVo) {
        // 取出所有的批次
        Map<ApsBatch, List<Order>> apsBatchListMap = apsOrderService.findApsBatch(reqVo);

        apsOrderService.reScheduling(reqVo, apsBatchListMap);

        return BaseResVo.ok();
    }


    @ApiOperation("海克思康-套料-推送")
    @PostMapping("/sendToMes")
    public BaseResVo sendToMes(@RequestBody ApsNestHexagonReqVo reqVo) {
//        reqVo.setCustName("PrdZMJ_2025");
//        reqVo.setCustCode("025");
////        reqVo.setCustAddr("Beijing");
//        reqVo.setMesOrderCode("001-Z24-258ZD");
//        reqVo.setOrderId(113);

        apsService.valid(reqVo);

        List<HexagonPackagePush> packagePushList = apsService.converterAndSave(reqVo);

        apsService.sendToMesAll(packagePushList);

        return BaseResVo.ok();
    }

    @ApiOperation("海克思康-套料-重新推送")
    @PostMapping("/resendToMes")
    public BaseResVo sendToMes(@RequestBody BaseIdReqVo reqVo) {
        HexagonPackagePush hexagonPackagePush = apsService.getByHexagonPushId(reqVo.getId());
        if (hexagonPackagePush == null) {
            throw SyExceptionUtils.e("包不存在");
        }

        if(hexagonPackagePush.getStatus() == 2 || hexagonPackagePush.getStatus() == 3){
            throw SyExceptionUtils.e("该工作包正在推送，或者已经推送成功");
        }

        ArrayList<HexagonPackagePush> objects = new ArrayList<>();
        objects.add(hexagonPackagePush);
        apsService.sendToMesAll(objects);
        return BaseResVo.ok();
    }

    @ApiOperation("海克思康-套料-包列表")
    @GetMapping("/hexagonPushList")
    public BaseListResVo<HexagonPushListResVo> hexagonPushList(BaseIdReqVo reqVo) {
        return apsService.hexagonPushList(reqVo);
    }

    @ApiOperation("海克思康-套料-包详情")
    @GetMapping("/hexagonPushDetail")
    public HexagonPushDetailResVo hexagonPushDetail(BaseIdReqVo reqVo) {
        return apsService.hexagonPushDetail(reqVo);
    }

    @ApiOperation("海克思康-订单列表")
    @GetMapping("/hexagonOrderList")
    public HexagonOrderListResVo<HexagonOrderListItemResVo> hexagonOrderList(HexagonOrderListReqVo reqVo) {
        return apsService.hexagonOrderList(reqVo);
    }

    @ApiOperation("海克思康-套料-预览包列表")
    @GetMapping("/previewHexagonPackage")
    public BaseListResVo<HexagonPreviewPackageResVo> previewHexagonPackage(BaseIdReqVo reqVo) {
        return apsService.previewHexagonPackage(reqVo);
    }


}
